package com.gc.ip;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JComponent;
import javax.swing.JLabel;


@SuppressWarnings("serial")
public class JLabelIPIndicator extends JLabel implements IPIndicator
{
	private PropertyChangeListener ipEditorListener = null;
	
	public JLabelIPIndicator(IPType ipType)
	{
		setIPType(ipType);
		ipEditorListener = new IPEditorPropertyChangeListener();
	}
	
	public JLabelIPIndicator()
	{
		this((IPType)null);
	}
	
	public void setIPType(IPType newVal)
	{
		if(newVal == IPType.IPv4)
		{
			setText(" V4 ");
		}
		else if(newVal == IPType.IPv6)
		{
			setText(" V6 ");
		}
		else
		{
			setText("  X  ");
		}
	}
	
	@Override
	public JComponent getUiComponent()
	{
		return this;
	}

	@Override
	public PropertyChangeListener getChangeListener(String propertyName)
	{
		return ipEditorListener;
	}

	private class IPEditorPropertyChangeListener implements PropertyChangeListener
	{
		@Override
		public void propertyChange(PropertyChangeEvent event)
		{
			String propName = event.getPropertyName();
			
			//IP Type changed
			if(IPEditor.IP_TYPE_PROPERTY.equals(propName))
			{
				IPType newVal = (IPType)event.getNewValue();
				setIPType(newVal);
			}
			else if(IPEditor.IP_ADDRESS_PROPERTY.equals(propName))
			{
				//Do something if needed
//				IPAddress newVal = (IPAddress)event.getNewValue();
//				setToolTipText("ToolTip: " + newVal);
			}
			else if(IPEditor.TEXT_PROPERTY.equals(propName))
			{
				//Do something if needed
//				String newVal = (String)event.getNewValue();
			}
		}
	}
}
